
//=============================================================
// Main entry of booting TianOS
// Version: 1.2
//-------------------------------------------------------------

(function () {
    "use strict";

    // Note: the canvas id must be 'weiran'
    var _theCanvas = document.getElementById('weiran');
    if (_theCanvas) {
        _theCanvas.style.background = "#E0F0F0 url('http://fanghuanweiran.googlecode.com/svn/trunk/loading.gif') no-repeat center";
    } else {
        Tian.Lang.add('en', 'notFoundCanvas', 'Oops, falled to get an element which id is "weiran".');
        Tian.Lang.add('en', 'notFoundCanvas', '糟糕, 未能找到一个以"weiran"为ID的页面元素.');
        alert(Tian.i18n('notFoundCanvas'));
        return
    }

    // set document.domain for apps and sensors accessing top resources
    //var domain = document.domain;
    //if (domain.indexOf('googlecode.com') >= 0) {
    //    document.domain = 'googlecode.com';
    //}

    // preload default theme
    Tian.requireCSS('http://fanghuanweiran.googlecode.com/svn/trunk/theme/default/style.css');
    
    var _jppHost = 'http://jpp.googlecode.com/svn/';
    
    // core libraries
    Tian.require([
        'http://openlayers.org/api/2.12/OpenLayers.js',
        _jppHost + 'socket.io/io-0.9.6.min.js'
    ], function() {
        _theCanvas.style.backgroundColor = '#D0E0E0';
        
        var libs = [];
        // lazy load soundmanager
        libs.push(_jppHost + 'soundmanager/soundmanager2.min.js');
        libs.push(_jppHost + 'openlayers/plugins/weolstk-0.4.min.js');
        if (typeof JSON === 'undefined') { // JSON2
            libs.push(_jppHost + 'json/json2.min.js');
        }
        // plugins and addons
        Tian.require(libs, function() {
            _theCanvas.style.backgroundColor = '#B5D0D0';
            _theCanvas.style.backgroundImage = 'none';
                
                // setup sound manager
            if (typeof soundManager !== 'undefined') {
                soundManager.setup({
                    url: 'http://www.fanghuanweiran.com/swf/',
                    flashLoadTimeout: 10000,
                    flashVersion: 9,
                    debugMode: false
                });
                soundManager.beginDelayedInit();
            }
            
            // create an instance of OS and run it
            var os = new Tian.OS('weiran', 'http://www.fanghuanweiran.com:2080');
            os.boot();
            
            var panel = os.getMainPanel();
            if (panel) {
                var _appHost = './apps/';
                // add apps
                panel.addApp(new Tian.App({
                    title: {'en': 'Account', 'zh-CN': '用户'},
                    icon: _appHost + 'account/icon.png', 
                    page: _appHost + 'account/index.html',
                    width: 540,
                    height: 300
                }));
                panel.addApp(new Tian.App({
                    title: {'en': 'Device Manager', 'zh-CN': '设备管理'},
                    icon: _appHost + 'device/icon.png', 
                    page: _appHost + 'device/index.html',
                    width: 540,
                    height: 300
                }));
                panel.addApp(new Tian.App({
                    title: {'en': 'Notification Center', 'zh-CN': '通知中心'},
                    icon: _appHost + 'notification/icon.png',
                    page: _appHost + 'notification/index.html',
                    width: 540,
                    height: 300
                }));
                panel.addApp(new Tian.App({
                    title: {'en': 'Layer Switcher', 'zh-CN': '图层切换'},
                    icon: _appHost + 'layerswitcher/icon.png',
                    page:  _appHost + 'layerswitcher/index.html',
                    width: 240,
                    height: 400
                }));
                panel.addApp(new Tian.App({
                    title: {'en': 'Feedback', 'zh-CN': '金口玉言'},
                    icon: _appHost + 'feedback/icon.png',
                    page: _appHost + 'feedback/index.html',
                    width: 540,
                    height: 300,
                }));
                /*
                panel.addApp(new Tian.App({
                    title: {'en': 'iWhere', 'zh-CN': '我在哪里'},
                    icon: _appHost + 'iwhere/icon.png',
                    page: _appHost + 'iwhere/index.html',
                    width: 200,
                    height: 160
                }));
                panel.addApp(new Tian.App({
                    title: {'en': 'LonLat Picker', 'zh-CN': '点取坐标'},
                    icon: _appHost + 'llpicker/icon.png',
                    page: _appHost + 'llpicker/index.html',
                    width: 260,
                    height: 160
                }));
                panel.addApp(new Tian.App({
                    title: {'en': 'Measuring Tools', 'zh-CN': '测量工具'},
                    icon: _appHost + 'measure/icon.png',
                    page: _appHost + 'measure/index.html',
                    width: 280,
                    height: 140,
                }));
                panel.addApp(new Tian.App({
                    title: {'en': 'Cell Tracker', 'zh-CN': '基站定位'},
                    icon: _appHost + 'celltracker/icon.png',
                    page: _appHost + 'celltracker/index.html',
                    width: 200,
                    height: 200
                }));*/
            }
             
            // add extra layers
            var map = os.getMap();
            if (map && typeof OpenLayers !== 'undefined') {
                map.addLayers([
                    // base layers
                    // overlay layers
                    // weather
                    new OpenLayers.Layer.WeWXRain(null, null, {os: os}),
                    new OpenLayers.Layer.WeWXWind(null, null, {os: os}),
                    new OpenLayers.Layer.WeWXTempAir(null, null, {os: os}),
                    new OpenLayers.Layer.WeWXTempSea(null, null, {os: os}),
                    new OpenLayers.Layer.WeWXSwellHeight(null, null, {os: os}),
                    new OpenLayers.Layer.WeWXSwellPeriod(null, null, {os: os}),
                    new OpenLayers.Layer.WeWXCloudIR(null, null, {os: os}),
                    new OpenLayers.Layer.WeWXCloudVisible(null, null, {os: os}),
                    new OpenLayers.Layer.WeWXCloudColored(null, null, {os: os}),
                    // earthquakes
                    new OpenLayers.Layer.WeEarthquakesHourly(),
                    new OpenLayers.Layer.WeEarthquakesDaily(),
                    new OpenLayers.Layer.WeEarthquakesWeekly()
                ]);
            }
        });
    });
}());
